package com.yiqi.lottery.feature.mine.view;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseFragment;
import com.suozhang.framework.utils.T;
import com.yiqi.lottery.R;
import com.yiqi.lottery.common.widget.dialog.ActionSheetDialog;
import com.yiqi.lottery.common.widget.dialog.OnOperItemClickL;
import com.yiqi.lottery.entity.bo.TokenBo;
import com.yiqi.lottery.feature.login.view.LoginActivity;
import com.yiqi.lottery.feature.mine.contract.MineContract;
import com.yiqi.lottery.feature.mine.dependencies.mine.DaggerMineComponent;
import com.yiqi.lottery.feature.mine.dependencies.mine.MinePresenterModule;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment implements MineContract.View, OnOperItemClickL {

    private static final int RESULT_OK = 0;
    @Inject
    MineContract.Presenter mPresenter;

    @BindView(R.id.iv_user_head)
    ImageView ivUserHead;
    @BindView(R.id.tv_user_edit_nick)
    TextView mTvUserEditNick;
    @BindView(R.id.bottom)
    LinearLayout mBottom;
    @BindView(R.id.tv_user_yue)
    TextView mTvUserYue;
    @BindView(R.id.tv_user_tixian)
    TextView mTvUserTixian;
    Unbinder unbinder1;
    private List<LocalMedia> selectList = new ArrayList<>();
    private String uri = "";
    private String mQiniuToken;
    private ActionSheetDialog dialog;
    UserInfoBo mUserInfoBo=null;


    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initInjector() {
        DaggerMineComponent.builder().minePresenterModule(new MinePresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        super.initView();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {

    }

    @Override
    public void onStart() {
        super.onStart();
        if (AM.user().isLogin()) {
            mPresenter.doGetUserInfo();
            mPresenter.postQiniuToken();
        } else {
            mTvUserEditNick.setText("未登录");
        }
    }

    @OnClick({R.id.btn_user_info, R.id.btn_user_recharge, R.id.btn_user_withdraw, R.id.btn_user_mseeage,
            R.id.btn_user_basicinfo, R.id.btn_user_bind_iphone, R.id.btn_user_verified,
            R.id.btn_user_betting_record, R.id.btn_user_tracing_management, R.id.btn_user_edit_pwd,
            R.id.btn_user_account_detail, R.id.btn_user_withdraw_setting, R.id.tv_user_edit_nick,
            R.id.btn_user_betting_setting, R.id.btn_user_setting})
    public void onViewClicked(View view) {

        if (view.getId() == R.id.btn_user_setting) {
            startActivity(new Intent(getActivity(), SeetingActivity.class));
            return;
        }

        if (!AM.user().isLogin()) {
            startActivity(new Intent(getActivity(), LoginActivity.class));
            return;
        }
        switch (view.getId()) {
            case R.id.btn_user_info://用户头像
                ActionSheetDialogNoTitle();
                break;
            case R.id.tv_user_edit_nick://修改昵称
                startActivity(new Intent(getActivity(), EditNickActivity.class));
                break;
            case R.id.btn_user_recharge://充值
                Intent intent=new Intent(getActivity(), RechargeActivity.class);
                intent.putExtra("user",mUserInfoBo);
                startActivity(intent);
                break;
            case R.id.btn_user_withdraw://提现
                Intent intent2=new Intent(getActivity(), WithdrawActivity.class);
                intent2.putExtra("user",mUserInfoBo);
                startActivity(intent2);
//                T.showShort("暂未开放！");

                break;
            case R.id.btn_user_mseeage://消息中心
                startActivity(new Intent(getActivity(), MessageActivity.class));
                break;
            case R.id.btn_user_basicinfo://基础资料
                startActivity(new Intent(getActivity(), BasicInfoActivity.class));
                break;
            case R.id.btn_user_bind_iphone://绑定手机
                startActivity(new Intent(getActivity(), EditPhoneActivity.class));
                break;
            case R.id.btn_user_verified://实名认证
                startActivity(new Intent(getActivity(), VerifiedActivity.class));
                break;
            case R.id.btn_user_betting_record://投注记录
                startActivity(new Intent(getActivity(), OrderActivity.class));
                break;
            case R.id.btn_user_tracing_management://追号管理
                startActivity(new Intent(getActivity(), NumberManageActivity.class));
                break;
            case R.id.btn_user_edit_pwd://修改密码
                startActivity(new Intent(getActivity(), EditPasswordActivity.class));
                break;
            case R.id.btn_user_account_detail://账户明细
                startActivity(new Intent(getActivity(), AccountInfoActivity.class));
                break;
            case R.id.btn_user_withdraw_setting://提现账户设置
                startActivity(new Intent(getActivity(), WithdrawalAccountSettingActivity.class));
                break;
            case R.id.btn_user_betting_setting://投注站基本信息
                startActivity(new Intent(getActivity(), BettingStationInfoActivity.class));
                break;

        }


    }

    @Override
    public void showUserInfoSuccess(UserInfoBo userInfoBo) {
        if (userInfoBo != null) {
            mUserInfoBo=userInfoBo;
            AM.image().bindToCircleObject(userInfoBo.getAvatar(), ivUserHead);
            mTvUserEditNick.setText(userInfoBo.getUsername());
            mTvUserYue.setText(userInfoBo.getBalance_amount() + "");
            mTvUserTixian.setText(userInfoBo.getAvailable_amount() + "");
        }
    }

    @Override
    public void showAuthSuccess(String msg) {
    }

    @Override
    public void showSendCodeSuccess(String code) {

    }

    @Override
    public void showEditPwdSuccess(String msg) {
        T.showShort(msg);
    }

    @Override
    public void showSendCodeError(String error) {
        T.showShort(error);
    }

    @Override
    public void showError(String error) {
        T.showShort(error);
    }

    @Override
    public void showQiniuTokenSuccess(TokenBo data) {
        mQiniuToken = data.getToken();
    }

    @Override
    public void showUploadHashSuccess(String hash) {
        if (AM.user().isLogin()) {
            mPresenter.editUserInfo(AM.user().getLoginResultCache().getUser_id(), hash);
        }

    }


    private void ActionSheetDialogNoTitle() {
        final String[] stringItems = {"拍照上传", "本地上传"};
        dialog = new ActionSheetDialog(getActivity(), stringItems, mBottom);
        dialog.isTitleShow(false).show();
        dialog.setOnOperItemClickL(this);
    }

    /**
     * 上传头像
     */
    @Override
    public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {

        switch (position) {
            case 0:
                PictureSelector.create(this)
                        .openCamera(PictureMimeType.ofImage())
                        .theme(R.style.picture_default_style1)
                        .selectionMode(PictureConfig.SINGLE)// 多选 or 单选
                        .previewImage(true)// 是否可预览图片
                        .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                        .enableCrop(true)// 是否裁剪
                        .compress(true)// 是否压缩
                        .hideBottomControls(false)// 是否显示uCrop工具栏，默认不显示
                        .isGif(false)// 是否显示gif图片
                        .freeStyleCropEnabled(true)// 裁剪框是否可拖拽
                        .circleDimmedLayer(true)// 是否圆形裁剪
                        .selectionMedia(selectList)// 是否传入已选图片
                        .forResult(PictureConfig.CHOOSE_REQUEST);
                break;
            case 1:
                PictureSelector.create(this)
                        .openGallery(PictureMimeType.ofImage())
                        .theme(R.style.picture_default_style1)
                        .selectionMode(PictureConfig.SINGLE)// 多选 or 单选
                        .previewImage(true)// 是否可预览图片
                        .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                        .isCamera(false)
                        .enableCrop(true)// 是否裁剪
                        .compress(true)// 是否压缩
                        .hideBottomControls(false)// 是否显示uCrop工具栏，默认不显示
                        .isGif(false)// 是否显示gif图片
                        .freeStyleCropEnabled(true)// 裁剪框是否可拖拽
                        .circleDimmedLayer(true)// 是否圆形裁剪
                        .selectionMedia(selectList)// 是否传入已选图片
                        .scaleEnabled(true)
                        .forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
                break;

        }

        if (dialog != null) {
            dialog.dismiss();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    selectList = PictureSelector.obtainMultipleResult(data);
                    for (int i = 0; i < selectList.size(); i++) {
                        uri = selectList.get(i).getCutPath();
                    }
                    mPresenter.uploadHead(uri, mQiniuToken);
                    break;
            }
        }
    }

}
